<?php
    header("Content-Type: text/html; charset=iso-8859-1");
    include "/includes/session.php";
    include "/includes/validaUsuario2.php";
    require_once "/includes/DAOs/UserDAO.php";
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="css/style.css" rel="stylesheet" type="text/css" />
		<script type="text/javascript">
			function validarFormulario(){
				var nombre_evento = document.getElementById('nombre_evento');
				var fecha_inicio = document.getElementById('fecha_inicio');
				var fecha_termino = document.getElementById('fecha_termino');
				var direccion_evento = document.getElementById('direccion_evento');
				var precios = document.getElementById('precios');
				var categoria = document.getElementById('id_categoria');
				var lat = document.getElementById('lat');
				var lon = document.getElementById('lon');
				
				if(nombre_evento.value == "" || fecha_inicio.value == "" || fecha_termino.value == "" || precios.value == "" || (direccion_evento.value == "" && (lat.value == "" || lon.value == ""))){
					alert('Rellene todos los campos');
				}
				else{
					fecha = fecha_inicio.value.split("-");
					fechaInicio = new Date();
					fechaInicio.setFullYear(fecha[0], fecha[1], fecha[2]);
					fecha = fecha_termino.value.split("-");
					fechaTermino = new Date()
					fechaTermino.setFullYear(fecha[0],fecha[1],fecha[2]);
					fechaActual = new Date();
					if(fechaTermino < fechaActual || fechaTermino < fechaInicio){
						alert('Error en Fecha Final');
					}
					else{
						document.forms["formularioEvento"].submit();
					}
				}
			}
		</script>
		<!-- INICIO CALENDARIO -->
		<script language="javascript" src="js/popcalendar.js"></script>		
		<!-- FIN CALENDARIO -->
		<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
		<script id="jqueryui" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" defer async></script>
		<script src="//www.google.com/jsapi?key=AIzaSyCZfHRnq7tigC-COeQRmoa9Cxr0vbrK6xw"></script>
		<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
		<script>
			var map;
			var geocoder; // = new google.maps.Geocoder();
			var tempMarker = new google.maps.Marker();
			var locationIcon = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_blue.png",
				new google.maps.Size(12,20),
				new google.maps.Size(6,20)
			);
			var newShadow = new google.maps.MarkerImage(
				"https://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_shadow.png",
				new google.maps.Size(22,20),
				new google.maps.Point(13,13)
			);
			function initialize() {
				geocoder = new google.maps.Geocoder();
				var mapOptions = {
					zoom: 16,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
				// Try HTML5 geolocation
				if(navigator.geolocation) {
					navigator.geolocation.getCurrentPosition(function(position) {
						var pos = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
						map.setCenter(pos);
					}, function() {
						handleNoGeolocation(true);
					});
				} else {
					// Browser doesn't support Geolocation
					handleNoGeolocation(false);
				}
				
				tempMarker.setOptions({
					icon: locationIcon,
					shadow: newShadow,
					draggable: true
				});
				
				google.maps.event.addListener(map, "click", showTempMarker);
				
				function showTempMarker(e) {
					tempMarker.setPosition(e.latLng);
					document.getElementById('lat').value = e.latLng.lat();
					document.getElementById('lon').value = e.latLng.lng();
					google.maps.event.addListener(tempMarker, "drag", function() {
						document.getElementById('lat').value = tempMarker.getPosition().lat();
						document.getElementById('lon').value = tempMarker.getPosition().lng();
					});
					tempMarker.setMap(map);
				}
			}
			
			function handleNoGeolocation(errorFlag) {
				if (errorFlag) {
					var content = 'Error: The Geolocation service failed.';
				} else {
					var content = 'Error: Your browser doesn\'t support geolocation.';
				}
				var options = {
					map: map,
					position: new google.maps.LatLng(-33.034355,-71.596313),
					content: content
				};
				var infowindow = new google.maps.InfoWindow(options);
				map.setCenter(options.position);
			}
			
			function showAddress(address) {
				geocoder.geocode({'address': address}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						var pos = results[0].geometry.location;
						map.setCenter(pos);
						tinyMarker = new google.maps.Marker({
							position: pos,
							map: map,
							icon: locationIcon,
							shadow: newShadow,
							draggable: true
						});
						document.getElementById('lat').value = pos.lat();
						document.getElementById('lon').value = pos.lng();
					} else {
						alert("POKE CHUPA EL PICO");
					}
				});
			}
			
			function buscar(){
				var aux = document.getElementById('address');
				var address = aux.value + ", Chile";
				showAddress(address);
			}
			
			google.maps.event.addDomListener(window, 'load', initialize);
		</script>
        <title>Go Now - Usuario</title>
    </head>
    <body>      
       <div class="image" style="width:968px;">		
		    <div class="user">
                <?php
                    $userDAO = new UserDAO();
                    $resultado = $userDAO->getNombreUsuario($_SESSION['id_user']);
                    list($nombre) = $userDAO->fetch($resultado);
                ?>
                <p>Usuario: <?php echo $nombre ?> <a href="includes/logout2.php">salir</a></p>
            </div>
			
			<a href="http://localhost/gonow/indexUser.php">
			<img  src="css/logo.png"  alt="logo"  style="margin-left:35px;"/> </a> 
		</div>
        
		<div id="contenido" class="content" style="min-height:850px;">
		  <div id="sidebar">
		  <ul>
		        <li style="background:#659C08;"><a href="indexUser.php">Mis Eventos</a></li> 
                <li><a href="agregarEvento.php">Agregar Evento</a></li>       
                <li><a href="misDatos.php">Mis Datos</a></li>
          </ul>				
          </div>
                <?php
					if(!isset($_GET['id'])){
						echo "<script type='text/javascript'> alert('No se recibio id'); document.location.href='http://localhost/gonow/indexUser.php';</script>";			
					}
					$userDAO = new UserDAO();
					$result = $userDAO->getEvento($_GET['id']);
					if ($userDAO->numRows($result) <= 0){
						echo "<script type='text/javascript'> alert('No existe el evento'); document.location.href='http://localhost/gonow/indexUser.php';</script>";			
					}
					list($nombre_evento, $fecha_inicio, $fecha_termino, $direccion_evento, $precios, $categoria, $latitud, $longitud) = $userDAO->fetch($result);
					$fecha_inicio = explode(" ", $fecha_inicio);
					$fecha_termino = explode(" ", $fecha_termino);
					$latitud = $latitud/1000000;
					$longitud = $longitud/1000000;
				?>
       <div style="float:right; width:750px;">
			<div style="float:right; width:420px;">
		     <div style="float: right; margin-right:20px; margin-top:40px; margin-bottom:20px;">
					<table>	
						<tr>
							<td><input type="text" id="address" name="address" value="<?php echo $direccion_evento; ?>" /></td>
							<td><input style="width: 100px; margin-top: -1px;" class="green_btn" onClick="buscar();" type="submit" id="Buscar" name="Buscar" value="Buscar"/></td>
						</tr>
					</table>
			</div>	
				<div id="map_canvas" style="  margin-right:20px; height: 450px; width: 400px;"></div>
			</div>
	  
            
			<div class="agregar-box" style="float:left;">
                <form id="formularioEvento" action="includes/controllers/userController.php?action=editarEvento" method="post">
				<input type='hidden' name="id_evento" value="<?php echo $_GET['id'] ?>" />
                        <p>Nombre del Evento</p>
                        <input type="text" name="nombre_evento" id="nombre_evento" value="<?php echo $nombre_evento ?>" />
						<p>Fecha Inicio (YYYY-MM-dd hh:mm:ss)</p>
                       <input type="text" name="fecha_inicio" id="fecha_inicio" value="<?php echo $fecha_inicio[0] ?>" onClick="popUpCalendar(this,formularioEvento.fecha_inicio,'yyyy-mm-dd')" style="margin-bottom:10px;"/>
						<select id="hora_inicio" name="hora_inicio">
							<option value="00:00:00" <?php if ($fecha_inicio[1] == '00:00:00'){ echo "selected"; } ?> >00:00 Hrs</option>
							<option value="01:00:00" <?php if ($fecha_inicio[1] == '01:00:00'){ echo "selected"; } ?>>01:00 Hrs</option>
							<option value="02:00:00" <?php if ($fecha_inicio[1] == '02:00:00'){ echo "selected"; } ?>>02:00 Hrs</option>
							<option value="03:00:00" <?php if ($fecha_inicio[1] == '03:00:00'){ echo "selected"; } ?>>03:00 Hrs</option>
							<option value="04:00:00" <?php if ($fecha_inicio[1] == '04:00:00'){ echo "selected"; } ?>>04:00 Hrs</option>
							<option value="05:00:00" <?php if ($fecha_inicio[1] == '05:00:00'){ echo "selected"; } ?>>05:00 Hrs</option>
							<option value="06:00:00" <?php if ($fecha_inicio[1] == '06:00:00'){ echo "selected"; } ?>>06:00 Hrs</option>
							<option value="07:00:00" <?php if ($fecha_inicio[1] == '07:00:00'){ echo "selected"; } ?>>07:00 Hrs</option>
							<option value="08:00:00" <?php if ($fecha_inicio[1] == '08:00:00'){ echo "selected"; } ?>>08:00 Hrs</option>
							<option value="09:00:00" <?php if ($fecha_inicio[1] == '09:00:00'){ echo "selected"; } ?>>09:00 Hrs</option>
							<option value="10:00:00" <?php if ($fecha_inicio[1] == '10:00:00'){ echo "selected"; } ?>>10:00 Hrs</option>
							<option value="11:00:00" <?php if ($fecha_inicio[1] == '11:00:00'){ echo "selected"; } ?>>11:00 Hrs</option>
							<option value="12:00:00" <?php if ($fecha_inicio[1] == '12:00:00'){ echo "selected"; } ?>>12:00 Hrs</option>
							<option value="13:00:00" <?php if ($fecha_inicio[1] == '13:00:00'){ echo "selected"; } ?>>13:00 Hrs</option>
							<option value="14:00:00" <?php if ($fecha_inicio[1] == '14:00:00'){ echo "selected"; } ?>>14:00 Hrs</option>
							<option value="15:00:00" <?php if ($fecha_inicio[1] == '15:00:00'){ echo "selected"; } ?>>15:00 Hrs</option>
							<option value="16:00:00" <?php if ($fecha_inicio[1] == '16:00:00'){ echo "selected"; } ?>>16:00 Hrs</option>
							<option value="17:00:00" <?php if ($fecha_inicio[1] == '17:00:00'){ echo "selected"; } ?>>17:00 Hrs</option>
							<option value="18:00:00" <?php if ($fecha_inicio[1] == '18:00:00'){ echo "selected"; } ?>>18:00 Hrs</option>
							<option value="19:00:00" <?php if ($fecha_inicio[1] == '19:00:00'){ echo "selected"; } ?>>19:00 Hrs</option>
							<option value="20:00:00" <?php if ($fecha_inicio[1] == '20:00:00'){ echo "selected"; } ?>>20:00 Hrs</option>
							<option value="21:00:00" <?php if ($fecha_inicio[1] == '21:00:00'){ echo "selected"; } ?>>21:00 Hrs</option>
							<option value="22:00:00" <?php if ($fecha_inicio[1] == '22:00:00'){ echo "selected"; } ?>>22:00 Hrs</option>
							<option value="23:00:00" <?php if ($fecha_inicio[1] == '23:00:00'){ echo "selected"; } ?>>23:00 Hrs</option>
						</select>
						<p>Fecha Termino (YYYY-MM-dd hh:mm:ss)</p>
                        <input type="text" name="fecha_termino" id="fecha_termino" value="<?php echo $fecha_termino[0] ?>" onClick="popUpCalendar(this,formularioEvento.fecha_termino,'yyyy-mm-dd')" style="margin-bottom:10px;"/>
						<select id="hora_inicio" name="hora_inicio">
							<option value="00:00:00" <?php if ($fecha_termino[1] == '00:00:00'){ echo "selected"; } ?> >00:00 Hrs</option>
							<option value="01:00:00" <?php if ($fecha_termino[1] == '01:00:00'){ echo "selected"; } ?>>01:00 Hrs</option>
							<option value="02:00:00" <?php if ($fecha_termino[1] == '02:00:00'){ echo "selected"; } ?>>02:00 Hrs</option>
							<option value="03:00:00" <?php if ($fecha_termino[1] == '03:00:00'){ echo "selected"; } ?>>03:00 Hrs</option>
							<option value="04:00:00" <?php if ($fecha_termino[1] == '04:00:00'){ echo "selected"; } ?>>04:00 Hrs</option>
							<option value="05:00:00" <?php if ($fecha_termino[1] == '05:00:00'){ echo "selected"; } ?>>05:00 Hrs</option>
							<option value="06:00:00" <?php if ($fecha_termino[1] == '06:00:00'){ echo "selected"; } ?>>06:00 Hrs</option>
							<option value="07:00:00" <?php if ($fecha_termino[1] == '07:00:00'){ echo "selected"; } ?>>07:00 Hrs</option>
							<option value="08:00:00" <?php if ($fecha_termino[1] == '08:00:00'){ echo "selected"; } ?>>08:00 Hrs</option>
							<option value="09:00:00" <?php if ($fecha_termino[1] == '09:00:00'){ echo "selected"; } ?>>09:00 Hrs</option>
							<option value="10:00:00" <?php if ($fecha_termino[1] == '10:00:00'){ echo "selected"; } ?>>10:00 Hrs</option>
							<option value="11:00:00" <?php if ($fecha_termino[1] == '11:00:00'){ echo "selected"; } ?>>11:00 Hrs</option>
							<option value="12:00:00" <?php if ($fecha_termino[1] == '12:00:00'){ echo "selected"; } ?>>12:00 Hrs</option>
							<option value="13:00:00" <?php if ($fecha_termino[1] == '13:00:00'){ echo "selected"; } ?>>13:00 Hrs</option>
							<option value="14:00:00" <?php if ($fecha_termino[1] == '14:00:00'){ echo "selected"; } ?>>14:00 Hrs</option>
							<option value="15:00:00" <?php if ($fecha_termino[1] == '15:00:00'){ echo "selected"; } ?>>15:00 Hrs</option>
							<option value="16:00:00" <?php if ($fecha_termino[1] == '16:00:00'){ echo "selected"; } ?>>16:00 Hrs</option>
							<option value="17:00:00" <?php if ($fecha_termino[1] == '17:00:00'){ echo "selected"; } ?>>17:00 Hrs</option>
							<option value="18:00:00" <?php if ($fecha_termino[1] == '18:00:00'){ echo "selected"; } ?>>18:00 Hrs</option>
							<option value="19:00:00" <?php if ($fecha_termino[1] == '19:00:00'){ echo "selected"; } ?>>19:00 Hrs</option>
							<option value="20:00:00" <?php if ($fecha_termino[1] == '20:00:00'){ echo "selected"; } ?>>20:00 Hrs</option>
							<option value="21:00:00" <?php if ($fecha_termino[1] == '21:00:00'){ echo "selected"; } ?>>21:00 Hrs</option>
							<option value="22:00:00" <?php if ($fecha_termino[1] == '22:00:00'){ echo "selected"; } ?>>22:00 Hrs</option>
							<option value="23:00:00" <?php if ($fecha_termino[1] == '23:00:00'){ echo "selected"; } ?>>23:00 Hrs</option>
						</select>
						<p>Direccion del Evento</p>
                        <input type="text" name="direccion_evento" id="direccion_evento" value="<?php echo $direccion_evento ?>" />
						<p>Precios</p>
                        <input type="text" name="precios" id="precios" value="<?php echo $precios ?>"/>
						<p>Categoria</p>
                        <select name="id_categoria" id="id_categoria">
											<?php
											$userDAO = new UserDAO();
											$result = $userDAO->getCategorias();
											while(list($id_categoria, $nombre_categoria) = $userDAO->fetch($result)){
											?>
												<option value="<?php echo $id_categoria; ?>" <?php if ($id_categoria == $categoria) {echo "selected";} ?> ><?php echo $nombre_categoria; ?></option>
											<?php
											}	
											?>
										</select>
						<p>Descripcion: </p>
						<?php 
										$userDAO2 = new UserDAO();
										$result2 = $userDAO2->getDescripcion($_GET['id']);
										if($userDAO->numRows($result2) > 0){
											list($id_descripcion, $descripcion) = $userDAO->fetch($result2);
									?>
									<input type='hidden' name="id_descripcion" value="<?php echo $id_descripcion ?>" />
									<textarea cols="28" rows="5" name="descripcion"><?php echo $descripcion ?></textarea>
										<?php } else {?>
											<textarea cols="28" rows="5" id="descripcion" name="descripcion"></textarea>
									<?php } ?>
						<p>Latitud </p>
                        <input type="text" id="lat" name="lat" size="15" value="<?php echo $latitud ?>" />
						<p>Longitud: </p>
						<input type="text" id="lon" name="lon" size="15" value="<?php echo $longitud ?>" />
                </form>
			</div>
				<input class="green_btn" type="button" onClick="javascript:validarFormulario();" value="Modificar" style="width:170px; margin-left:110px; margin-top:30px; "/>
				<input class="orange_btn" type="button" onClick="document.location.href='includes/controllers/userController.php?action=eliminarEvento&id=<?php echo $_GET['id']?>'" value="Eliminar" style="width:70px; margin-left:10px; margin-top:30px; "/>
        

		</div>		
        </div>
    </body> 
	<footer>		
		<p> Proyecto Seminario II Telem&aacute;tica 2012 &copy Gonz&aacute;lez - Orozco - Pino - Villarreal</p>
    </footer>
</html>
